<template>
  <a-modal
    :maskClosable="maskClosable"
    title="控制方式"
    :width="600"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-form-item
          label="功耗控制方式"
        >
          <a-radio-group v-model="model.controlType">
            <a-radio value="0">自动</a-radio>
            <a-radio value="1">手动</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          v-if="model.controlType > 0"
          label="调频策略"
        >
          <a-radio-group v-model="model.voltageFmStrategy">
            <a-radio
              v-for="(d, i) in model.supportStrategies"
              :key="i"
              :value="d"
            >
              {{ d }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import { gPostAction } from "@/api/manage";
export default {
  name: "ConfigModal",
  data() {
    return {
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },
      radioStyle: {},
      maskClosable: false,
    };
  },
  methods: {
    config(record) {
      this.model = Object.assign({}, record);
      this.model.controlType = this.model.controlType + "";
      this.visible = true;
    },
    handleOk(e) {
      this.confirmLoading = true;
      const postData = {
        id: this.model.id,
        controlType: this.model.controlType,
        title: "精确管控与供电制冷联动-精确能耗管控-计算节点控制",
        content: "设置控制方式"
      };

      if (this.model.controlType > 0) {
        postData.voltageFmStrategy = this.model.voltageFmStrategy;
      }

      gPostAction("/fm/setStrategy", postData)
        .then((res) => {
          if (res.success) {
            this.$message.success("操作成功");
            this.$emit("ok");
          }
        })
        .finally(() => {
          this.confirmLoading = false;
          this.handleCancel();
        });
    },
    handleCancel(e) {
      this.visible = false;
    },
  },
};
</script>
